//
//  XVCAppontmentDetail.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/11.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVCAppontmentDetail.h"

#import "XTCAppointmentDetailPersonShow.h"
#import "ShowTipAndContentClickCell.h"
#import "XXTool.h"
#import "XVCRefuseVisitor.h"
#import "XMLVisitorAppointDetailModel.h"
#import "LiuyanViewController.h"

@interface XVCAppontmentDetail ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

@property (nonatomic) UITableView* tableview;

@property (nonatomic) DKButton *refuseButton;

@property (nonatomic) DKButton *acceptButton;

@property (nonatomic) UILabel *alreadyAcceptedButton;

@property(nonatomic,strong) UIButton * liuyan;

@property(nonatomic) Boolean needFlush;

@end

@implementation XVCAppontmentDetail
{
    NSMutableArray <XMLNameAndValueModel*> *useData;
    
    XMLAppointmentDetail *personData;
    
}
-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        MJRefreshBackNormalFooter *useFooter = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerFresh)];
        [useFooter setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useFooter setTitle:NSLocalizedString(@"上拉刷新", nil) forState:MJRefreshStateIdle];
        [useFooter setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_footer = useFooter;
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

-(UIButton*)liuyan{
    if(_liuyan == nil){
        _liuyan=[[UIButton alloc]init];
        _liuyan.titleLabel.font = [XXTool systemFontOfSize:16.5f];
        _liuyan.backgroundColor=[XXTool colorWithProject];
        [_liuyan setTitle:NSLocalizedString(@"留言", nil) forState:UIControlStateNormal];
        [_liuyan setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_liuyan addTarget:self action:@selector(clickLiuyanButton) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:_liuyan];
    }
    return  _liuyan;
}

-(DKButton*)refuseButton
{
    if (_refuseButton==nil)
    {
        _refuseButton = [[DKButton alloc] init];
        _refuseButton.text = NSLocalizedString(@"拒绝", nil);
        _refuseButton.textColor = [XXTool colorWithProject];
        _refuseButton.layer.borderColor = [XXTool colorWithProject].CGColor;
        _refuseButton.layer.cornerRadius = 4;
        _refuseButton.layer.borderWidth = 1.8f;
        _refuseButton.font = [XXTool systemFontOfSize:16.5f];
        [_refuseButton setColor:[UIColor whiteColor] ForState:DKButtonStateNormal];
        [_refuseButton addTarget:self Select:@selector(clickRefuseButton) ForEvent:DKButtonEventPressedEnd];
        [self.view addSubview:_refuseButton];
    }
    return _refuseButton;
}

-(DKButton*)acceptButton
{
    if (_acceptButton==nil)
    {
        _acceptButton = [[DKButton alloc] init];
        _acceptButton.text = NSLocalizedString(@"同意", nil);
        _acceptButton.textColor = [UIColor whiteColor];
        _acceptButton.layer.cornerRadius = 4;
        _acceptButton.font = [XXTool systemFontOfSize:16.5f];
        [_acceptButton setColor:[XXTool colorWithProject] ForState:DKButtonStateNormal];
        [_acceptButton addTarget:self Select:@selector(clickAcceptButton) ForEvent:DKButtonEventPressedEnd];
        [self.view addSubview:_acceptButton];
    }
    return _acceptButton;
}

-(UILabel*)alreadyAcceptedButton
{
    if (_alreadyAcceptedButton==nil)
    {
        _alreadyAcceptedButton = [[UILabel alloc] init];
        _alreadyAcceptedButton.textColor = [UIColor whiteColor];
        _alreadyAcceptedButton.backgroundColor = [XXTool colorWithHexString:@"C2C2C2"];
        _alreadyAcceptedButton.layer.cornerRadius = 4;
        _alreadyAcceptedButton.font = [XXTool systemFontOfSize:16.5f];
        _alreadyAcceptedButton.text = NSLocalizedString(@"已同意", nil);
        _alreadyAcceptedButton.textAlignment = NSTextAlignmentCenter;
        _alreadyAcceptedButton.clipsToBounds = YES;
        [self.view addSubview:_alreadyAcceptedButton];
    }
    return _alreadyAcceptedButton;
}

-(void)initParameter
{
    
}

-(void)initUI
{
    [self.navigationItem setTitle:NSLocalizedString(@"预约详情", nil)];
    [self tableview];
    [self refuseButton];
    [self acceptButton];
    [self alreadyAcceptedButton];
    [self liuyan];
    
    if(_useType == XVCExhiLiuiyan){
        _refuseButton.hidden=YES;
        _acceptButton.hidden=YES;
        _alreadyAcceptedButton.hidden=YES;
        [self.navigationItem setTitle:NSLocalizedString(@"留言详情", nil)];
    }
    useData = [[NSMutableArray alloc] init];
    _needFlush=true;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if(_needFlush){
        [self initData];
    }
    
}

/**
 * 点击留言
 */
-(void)clickLiuyanButton{
    _needFlush=true;
    if(UserInfo.shareUserInfo.isExhibitor){
        [LiuyanViewController pushVistorLiuyanDetailWithSourceVC:self apmentID:self.appointmentPersonID Type:XVCExhi];
    }else{
        [LiuyanViewController pushVistorLiuyanDetailWithSourceVC:self apmentID:self.appointmentPersonID Type:XVCVisitor];
    }}

-(void)initData
{
    [self.view startLoading];
    _needFlush=false;
    if(_useType==XVCExhiLiuiyan){
        [XMLAppointmentDetail requestExhiLiuyanDetailWithAppointmentId:useData.count ID:self.appointmentPersonID completion:^(BOOL isSuccess, XMLAppointmentDetail *data,NSMutableArray<LiuyanDetailModel *>* dataList){
            if (isSuccess)
            {
                personData = data;
                //useData = [[NSMutableArray alloc] init];
                
                for (int i=0;i<dataList.count;i++)
                {
                    XMLNameAndValueModel *one = [[XMLNameAndValueModel alloc] init];
                    if([dataList[i].mstype isEqualToString:@"1"]){
                        one.name = NSLocalizedString(@"观众留言", nil);
                    }else{
                        one.name = NSLocalizedString(@"展商留言", nil);
                        
                    }
                    one.value = NSLocalizedString(dataList[i].content,nil);
                    [useData addObject:one];
                }
                [_tableview reloadData];
            }
            [self.view stopLoading];
        }];
        
    }else{
        [XMLAppointmentDetail requestAppointmentDetailWithOrderId:self.appointmentPersonID
                                                       completion:^(BOOL isSuccess, XMLAppointmentDetail *detail) {
                                                           if (isSuccess)
                                                           {
                                                               personData = detail;
                                                               
                                                               useData = [[NSMutableArray alloc] init];
                                                               
                                                               XMLNameAndValueModel *one = [[XMLNameAndValueModel alloc] init];
                                                               one.name = NSLocalizedString(@"预约时间", nil);
                                                               one.value = [XXTool stampToTime:personData.order_time];
                                                               [useData addObject:one];
                                                               
                                                               XMLNameAndValueModel *two = [[XMLNameAndValueModel alloc] init];
                                                               two.name = NSLocalizedString(@"预约留言", nil);
                                                               two.value = personData.value;
                                                               [useData addObject:two];
                                                               
                                                               [_tableview reloadData];
                                                               
                                                               if ([personData.orderaccept intValue]==1)
                                                               {
                                                                   [self buttonStateWithIsAccept:YES];
                                                               }
                                                               else
                                                               {
                                                                   [self buttonStateWithIsAccept:NO];
                                                               }
                                                           }
                                                           [self.view stopLoading];
                                                       }];
 
    }
}

-(void)footerFresh
{
    if(_useType==XVCExhiLiuiyan){
        [XMLAppointmentDetail requestExhiLiuyanDetailWithAppointmentId:useData.count ID:self.appointmentPersonID completion:^(BOOL isSuccess, XMLAppointmentDetail *data,NSMutableArray<LiuyanDetailModel *>* dataList){
            if (isSuccess)
            {
                personData = data;
                
                for (int i=0;i<dataList.count;i++)
                {
                    XMLNameAndValueModel *one = [[XMLNameAndValueModel alloc] init];
                    if([dataList[i].mstype isEqualToString:@"1"]){
                        one.name = NSLocalizedString(@"观众留言", nil);
                    }else{
                        one.name = NSLocalizedString(@"展商留言", nil);
                        
                    }
                    one.value = NSLocalizedString(dataList[i].content,nil);
                    [useData addObject:one];
                }
                [_tableview reloadData];
            }
            [_tableview.mj_footer endRefreshing];
        }];
    }
    else
    {
        [_tableview.mj_footer endRefreshing];
    }
}


-(void)refreshLayout:(CGRect)frame
{
    if(_useType == XVCExhiLiuiyan){
        _tableview.frame = CGRectMake(0, 0, frame.size.width, frame.size.height-45);
        _liuyan.hidden=NO;
        _liuyan.frame=CGRectMake(0, _tableview.frame.size.height, frame.size.width, 45);

    }else{
        _tableview.frame = CGRectMake(0, 0, frame.size.width, frame.size.height-60);
        _alreadyAcceptedButton.frame = CGRectMake(5
                                                  , _tableview.frame.size.height+_tableview.frame.origin.y+10
                                                  , frame.size.width-10
                                                  , 40);
        _refuseButton.frame = CGRectMake(5
                                         , _alreadyAcceptedButton.frame.origin.y
                                         , (frame.size.width-10-6)/2
                                         , _alreadyAcceptedButton.frame.size.height);
        _acceptButton.frame = CGRectMake(_refuseButton.frame.size.width+_refuseButton.frame.origin.x+6
                                         , _alreadyAcceptedButton.frame.origin.y
                                         , _refuseButton.frame.size.width
                                         , _refuseButton.frame.size.height);
        _liuyan.hidden=YES;
    }
    
}

#pragma mark tableviewDatasource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
    {
        XTCAppointmentDetailPersonShow *cell = [XTCAppointmentDetailPersonShow cellSelectionStyleNoneWithTableView:tableView];
        [cell setCellWithData:personData personType:XVVReleasePersonInfomationDetail];
        return cell;
    }
    else
    {
        ShowTipAndContentClickCell *cell = [ShowTipAndContentClickCell cellSelectionStyleNoneWithTableView:tableView];
        [cell setCellWithData:useData[indexPath.row-1]];
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
    {
         return [XTCAppointmentDetailPersonShow getHeight:personData personType:XVVReleasePersonInfomationDetail];
    }
    return [ShowTipAndContentClickCell getCellHeightWithData:useData[indexPath.row-1]];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return useData.count+1;
}

#pragma mark tableviewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

/**
 *  是否已接受
 *
 *  @param isAccept bool
 */
-(void)buttonStateWithIsAccept:(BOOL)isAccept
{
    if (isAccept)
    {
        _alreadyAcceptedButton.hidden = NO;
        [self.view bringSubviewToFront:_alreadyAcceptedButton];
        _refuseButton.hidden = YES;
        _acceptButton.hidden = YES;
    }
    else
    {
        _alreadyAcceptedButton.hidden = YES;
        _refuseButton.hidden = NO;
        _acceptButton.hidden = NO;
        [self.view bringSubviewToFront:_refuseButton];
        [self.view bringSubviewToFront:_acceptButton];
    }
}

-(void)clickAcceptButton
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"提示", nil)
                                                    message:NSLocalizedString(@"是否确定同意预约", nil)
                                                   delegate:self
                                          cancelButtonTitle:NSLocalizedString(@"取消", nil)
                                          otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
    [alter show];
}

-(void)clickRefuseButton
{
    [XVCRefuseVisitor pushRefuseVisitorVCWithSourceVC:self
                                           apPersonID:personData.orderid completion:^{
                                               NSMutableArray *viewcontrollers = [self.navigationController.viewControllers mutableCopy];
                                               [viewcontrollers removeLastObject];
                                               [viewcontrollers removeLastObject];
                                               [self.navigationController setViewControllers:viewcontrollers animated:YES];
                                           }];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1)
    {
        [DKHTTPRequest sendDataWithShortUrl:@"msg_accept"
                                      Param:[XXTool formatStringWithLanguage:[NSString stringWithFormat:@"userid=%@&passwd=%@&orderid=%@&accept=1",[UserInfo shareUserInfo].userID,[UserInfo shareUserInfo].userPassword,self.appointmentPersonID]]
                                  ImageArray:nil
                                     TimeOut:10
                                  Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                      if ([[serverDic objectForKey:@"code"] intValue]==200)
                                      {
                                          [self buttonStateWithIsAccept:YES];
                                          if (self.useBlock)
                                          {
                                              self.useBlock();
                                          }
                                      }
                                      return YES;
                                  } IsBuffering:NO
                           IsNeedReplaceWrap:NO
                             IsUseUpLoadHead:NO];
    }
}


#pragma mark api
+(void)pushAppontmentDetailVCWithSourceVC:(UIViewController *)sourceVC apPersonID:(NSString *)apID completion:(void (^)())block
{
    XVCAppontmentDetail *vc = [[XVCAppontmentDetail alloc] init];
    vc.appointmentPersonID = apID;
    vc.useBlock = block;
    vc.useType=XVCExhiYuyue;
    if (sourceVC)
    {
        [sourceVC.navigationController pushViewController:vc animated:YES];
    }
}

+(void)pushLiuyanDetailVCWithSourceVC:(UIViewController *)sourceVC apPersonID:(NSString *)apID{
    XVCAppontmentDetail *vc = [[XVCAppontmentDetail alloc] init];
    vc.appointmentPersonID = apID;
    vc.useType=XVCExhiLiuiyan;
    if (sourceVC)
    {
        [sourceVC.navigationController pushViewController:vc animated:YES];
    }

}

@end
